package Servlet;

import DAO.DAOFactory;
import DAO.MaintenanceDAO;
import javabean.Maintenance;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/DistributionMaintance")
public class DistributionMaintanceServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding( "UTF-8" );
        resp.setCharacterEncoding( "UTF-8" );
        System.out.println("maintenanceID is " + req.getParameter("maintenanceID"));
        System.out.println("technicianID is " + req.getParameter("technicianID"));
        int maintenanceID = Integer.parseInt(req.getParameter("maintanceID"));
        String technicianID = req.getParameter("technicianID");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        MaintenanceDAO maintenanceDAO = (MaintenanceDAO) DAOFactory.getMaintenanceDAOInstance();
        try {
            Maintenance maintenance = maintenanceDAO.getMaintenanceByID(maintenanceID);
            maintenanceDAO.distributionMaintenance(maintenance,technicianID);
            out.print("<script>alert('分配成功！');window.location.href='UnassignedMaintance'</script>");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
